<template>
    <el-container>
        <el-main>
            <el-row>
                <el-col :span="12" :offset="6">

                    <el-card class="box-card">
                        <div slot="header" class="clearfix">
                            <span>用户登录</span>
                        </div>
                        <div>
                            <el-form ref="loginInfo" label-width="80px">
                                <el-form-item label="用户名">
                                    <el-input v-model="loginInfo.username"></el-input>
                                </el-form-item>
                                <el-form-item label="密码">
                                    <el-input v-model="loginInfo.password"></el-input>
                                </el-form-item>
                                <el-form-item>
                                    <el-button @click.prevent="login" type="primary">登录</el-button>
                                </el-form-item>
                            </el-form>
                        </div>
                    </el-card>

                </el-col>
            </el-row>
        </el-main>
    </el-container>
</template>

<script>
    import VueCookies from 'vue-cookies'

    export default {
        name: "Login",
        data() {
            return {
                loginInfo: {
                    username: '',
                    password: ''
                }
            }
        },
        methods: {
            login() {
                this.axios.post('/login', this.loginInfo)
                    .then(resp => {
                        // 1. 登录成功，首先要将token设置到cookie中
                        // 2. 跳转到首页
                        let token = resp.data.token;
                        window.console.log(token);
                        VueCookies.set('token', token);
                        // 实现页面的跳转
                        this.$router.push({
                            path: '/index'
                        })
                    })
            }
        }
    }
</script>

<style scoped>

</style>